#include "configs.h"
#include <QDebug>
#include <QSettings>


SerialConfigs::SerialConfigs()
{

}

void SerialConfigs::setSerialInfo(QString serial, int baud, char parity, int databits, int stopbits)
{
    this->setSerialName(serial);
    this->setSerialBaud(baud);
    this->setSerialParity(parity);
    this->setSerialDataBits(databits);
    this->setSerialStopBits(stopbits);
}

void SerialConfigs::setSerialName(QString serial)
{
    this->Serial=serial;
}

void SerialConfigs::setSerialBaud(int baud)
{
    this->Baud=baud;
}

void SerialConfigs::setSerialParity(char parity)
{
    this->Parity=parity;
}

void SerialConfigs::setSerialDataBits(int databits)
{
    this->DataBits=databits;
}

void SerialConfigs::setSerialStopBits(int stopbits)
{
    this->StopBits=stopbits;
}

QString SerialConfigs::getSerialName()
{
    return this->Serial;
}

int SerialConfigs::getSerialBaud()
{
    return this->Baud;
}

char SerialConfigs::getSerialParity()
{
    return this->Parity;
}

int SerialConfigs::getSerialDataBits()
{
    return this->DataBits;
}

int SerialConfigs::getSerialStopBits()
{
    return this->StopBits;
}

void SerialConfigs::getSerialFromTW(QTableWidget *tw, int row)
{
    QString name=tw->item(row,0)->data(0).toString();
    int baud=tw->item(row,1)->data(0).toInt();
    int databits=tw->item(row,2)->data(0).toInt();
    char parity=tw->item(row,3)->data(0).toString().at(0).toLatin1();
    int stopbits=tw->item(row,4)->data(0).toInt();
    setSerialInfo(name,baud,parity,databits,stopbits);
}

void SerialConfigs::setSerialToTW(QTableWidget *tw, int row)
{
    tw->insertRow(tw->rowCount());
    tw->setItem(row,0,new QTableWidgetItem(this->Serial));
    tw->setItem(row,1,new QTableWidgetItem(QString::number(this->Baud)));
    tw->setItem(row,2,new QTableWidgetItem(QString::number(this->DataBits)));
    tw->setItem(row,3,new QTableWidgetItem(QString(this->Parity)));
    tw->setItem(row,4,new QTableWidgetItem(QString::number(this->StopBits)));
}



void SerialConfigs::saveSerialIni(QString file,int row)
{
    QSettings setting(file,QSettings::IniFormat);
    setting.beginWriteArray("SerialConfig");
    setting.setArrayIndex(row);
    setting.setValue("SerianName",this->Serial);
    setting.setValue("SerialBaud",this->Baud);
    setting.setValue("SerialParity",QString(this->Parity));
    setting.setValue("SerialDataBits",this->DataBits);
    setting.setValue("SerialStopBits",this->StopBits);
    setting.endArray();
}

void SerialConfigs::loadSerialIni(QString file, int row)
{
    QSettings setting(file,QSettings::IniFormat);
    setting.beginReadArray("SerialConfig");
    setting.setArrayIndex(row);
    setSerialName(setting.value("SerianName").toString());
    setSerialBaud(setting.value("SerialBaud").toInt());
    setSerialParity(setting.value("SerialParity").toString().at(0).toUpper().toLatin1());
    setSerialDataBits(setting.value("SerialDataBits").toInt());
    setSerialStopBits(setting.value("SerialStopBits").toInt());
    //setting.setValue("SerianName",this->Serial);
    //setting.setValue("SerialBaud",this->Baud);
    //setting.setValue("SerialParity",QString(this->Parity));
    //setting.setValue("SerialDataBits",this->DataBits);
    //setting.setValue("SerialStopBits",this->StopBits);
    setting.endArray();
}

void SerialConfigs::insertIntoInputs(InputConfigs *ic)
{
    this->inputs.append(ic);
}

void SerialConfigs::insertIntoOutputs(OutputConfigs *oc)
{
    this->outputs.append(oc);
}

bool tog=true;

void SerialConfigs::loadFromSerials(QString file, QList<SerialConfigs *> *serials)
{
    QSettings setting(file,QSettings::IniFormat);
    int i;
    int cnt=setting.beginReadArray("SerialConfig");
    serials->clear();
    for(i=0;i<cnt;i++)
    {
        setting.setArrayIndex(i);
        SerialConfigs *sc=new SerialConfigs();
        QString name=setting.value("SerianName").toString();
        int baud=setting.value("SerialBaud").toInt();
        int databits=setting.value("SerialDataBits").toInt();
        char parity=setting.value("SerialParity").toString().at(0).toUpper().toLatin1();
        int stopbits=setting.value("SerialStopBits").toInt();
        sc->setSerialInfo(name,baud,parity,databits,stopbits);
        serials->append(sc);
    }
}

void SerialConfigs::loadFromSerials(QString file, QTableWidget *tw, QList<SerialConfigs *> *serials)
{
    QSettings setting(file,QSettings::IniFormat);
    int i;
    int cnt=setting.beginReadArray("SerialConfig");
    serials->clear();
    for(i=0;i<cnt;i++)
    {
        setting.setArrayIndex(i);
        SerialConfigs *sc=new SerialConfigs();
        QString name=setting.value("SerianName").toString();
        int baud=setting.value("SerialBaud").toInt();
        int databits=setting.value("SerialDataBits").toInt();
        char parity=setting.value("SerialParity").toString().at(0).toUpper().toLatin1();
        int stopbits=setting.value("SerialStopBits").toInt();
        sc->setSerialInfo(name,baud,parity,databits,stopbits);
        serials->append(sc);
        tw->insertRow(tw->rowCount());
        tw->setItem(i,0,new QTableWidgetItem(name));
        tw->setItem(i,1,new QTableWidgetItem(QString::number(baud)));
        tw->setItem(i,2,new QTableWidgetItem(QString::number(databits)));
        tw->setItem(i,3,new QTableWidgetItem(QString(parity)));
        tw->setItem(i,4,new QTableWidgetItem(QString::number(stopbits)));

    }
}

void SerialConfigs::saveToSerials(QString file,QTableWidget *tw)
{
    int cnt=tw->rowCount(),i=0;
    QString name;
    int baud,databits,stopbits;
    char parity;
    QSettings setting(file,QSettings::IniFormat);
    setting.beginWriteArray("SerialConfig");

    for(i=0;i<cnt;i++)
    {
        if(tw->item(i,0)==NULL||tw->item(i,3)==NULL)
        {
            tw->removeRow(i);
            continue;
        }
        name=tw->item(i,0)->data(0).toString();
        baud=tw->item(i,1)->data(0).toInt();
        databits=tw->item(i,2)->data(0).toInt();
        parity=tw->item(i,3)->data(0).toString().at(0).toLatin1();
        stopbits=tw->item(i,4)->data(0).toInt();
        setting.setArrayIndex(i);
        setting.setValue("SerianName",name);
        setting.setValue("SerialBaud",baud);
        setting.setValue("SerialParity",QString(parity));
        setting.setValue("SerialDataBits",databits);
        setting.setValue("SerialStopBits",stopbits);
    }
    setting.endArray();
}



int InputConfigs::getConnect() const
{
    return connect;
}

void InputConfigs::setConnect(int value)
{
    connect = value;
}

InputConfigs::InputConfigs()
{
    connect=0;
}



void InputConfigs::setInputInfo(QString serialname,int id, int srcAddr, int length, int dstAddr, QString info)
{
    setInputSerialName(serialname);
    setInputSlaveID(id);
    setInputDataAddress(srcAddr);
    setInputDataLength(length);
    setInputSaveAddress(dstAddr);
    setInputInfoStr(info);
}

void InputConfigs::setInputSerialName(QString name)
{
    this->SerialName=name;
}

void InputConfigs::setInputSlaveID(int id)
{
    this->SlaveID=id;
}

void InputConfigs::setInputDataAddress(int srcAddr)
{
    this->DataAddress=srcAddr;
}

void InputConfigs::setInputDataLength(int length)
{
    this->DataLength=length;
}

void InputConfigs::setInputSaveAddress(int dstAddr)
{
    this->SaveAddress=dstAddr;
}

void InputConfigs::setInputInfoStr(QString info)
{
    this->infoStr=info;
}

QString InputConfigs::getInputSerialName()
{
    return this->SerialName;
}

int InputConfigs::getInputSlaveID()
{
    return this->SlaveID;
}

int InputConfigs::getInputDataAddress()
{
    return this->DataAddress;
}

int InputConfigs::getInputDataLength()
{
    return this->DataLength;
}

int InputConfigs::getInputSaveAddress()
{
    return this->SaveAddress;
}

QString InputConfigs::getInputInfoStr()
{
    return this->infoStr;
}

void InputConfigs::statechange()
{
    //this->connect++;
    emit this->stateToggle(this->listIcon);
}

void InputConfigs::connected()
{
    connect++;
    if(connect>10000)
        connect=10000;
}

void InputConfigs::loadFromInputs(QString file, QList<InputConfigs *> *inputs)
{
    QSettings setting(file,QSettings::IniFormat);
    int i;
    int cnt=setting.beginReadArray("InputConfig");
    inputs->clear();
    for(i=0;i<cnt;i++)
    {
        setting.setArrayIndex(i);
        InputConfigs *ic=new InputConfigs();
        QString name=setting.value("InputSerial").toString();
        int id=setting.value("InputSlaveID").toInt();
        int srcAddr=setting.value("InputDataAddress").toInt();
        int length=setting.value("InputDataLength").toInt();
        int dstAddr=setting.value("InputSaveAddress").toInt();
        QString infostr=setting.value("InputInfoStr").toString();
        ic->setInputInfo(name,id,srcAddr,length,dstAddr,infostr);
        inputs->append(ic);
    }
}

void InputConfigs::loadFromInputs(QString file, QTableWidget *tw, QList<InputConfigs *> *inputs)
{
    QSettings setting(file,QSettings::IniFormat);
    int i;
    int cnt=setting.beginReadArray("InputConfig");
    inputs->clear();
    for(i=0;i<cnt;i++)
    {
        setting.setArrayIndex(i);
        InputConfigs *ic=new InputConfigs();
        QString name=setting.value("InputSerial").toString();
        int id=setting.value("InputSlaveID").toInt();
        int srcAddr=setting.value("InputDataAddress").toInt();
        int length=setting.value("InputDataLength").toInt();
        int dstAddr=setting.value("InputSaveAddress").toInt();
        QString infostr=setting.value("InputInfoStr").toString();
        ic->setInputInfo(name,id,srcAddr,length,dstAddr,infostr);
        inputs->append(ic);
        tw->insertRow(tw->rowCount());
        tw->setItem(i,0,new QTableWidgetItem(name));
        tw->setItem(i,1,new QTableWidgetItem(QString::number(id)));
        tw->setItem(i,2,new QTableWidgetItem(QString::number(srcAddr)));
        tw->setItem(i,3,new QTableWidgetItem(QString::number(length)));
        tw->setItem(i,4,new QTableWidgetItem(QString::number(dstAddr)));
        tw->setItem(i,5,new QTableWidgetItem(infostr));
    }
}

void InputConfigs::saveToInputs(QString file, QTableWidget *tw)
{
    int cnt=tw->rowCount(),i=0;
    QString infostr,port;
    int id,srcAddr,length,dstAddr;
    QSettings setting(file,QSettings::IniFormat);
    setting.beginWriteArray("InputConfig");
    for(i=0;i<cnt;i++)
    {
        if(tw->item(i,0)==NULL||tw->item(i,5)==NULL)
        {
            tw->removeRow(i);
            continue;
        }
        port=tw->item(i,0)->data(0).toString();
        id=tw->item(i,1)->data(0).toInt();
        srcAddr=tw->item(i,2)->data(0).toInt();
        length=tw->item(i,3)->data(0).toInt();
        dstAddr=tw->item(i,4)->data(0).toInt();
        infostr=tw->item(i,5)->data(0).toString();
        setting.setArrayIndex(i);
        setting.setValue("InputSerial",port);
        setting.setValue("InputSlaveID",id);
        setting.setValue("InputDataAddress",srcAddr);
        setting.setValue("InputDataLength",length);
        setting.setValue("InputSaveAddress",dstAddr);
        setting.setValue("InputInfoStr",infostr);
    }
    setting.endArray();
}




int OutputConfigs::getConnect() const
{
    return connect;
}

void OutputConfigs::setConnect(int value)
{
    connect = value;
}

OutputConfigs::OutputConfigs()
{

}

void OutputConfigs::setOutputInfo(QString serialname, int id, int srcAddr, int length,int startAddr)
{
    setOutputSerialName(serialname);
    setOutputSlaveID(id);
    setOutputDataAddress(srcAddr);
    setOutputDataLength(length);
    setOutputStartAddr(startAddr);
}

void OutputConfigs::setOutputSerialName(QString name)
{
    this->SerialName=name;
}

void OutputConfigs::setOutputSlaveID(int id)
{
    this->SlaveID=id;
}

void OutputConfigs::setOutputDataAddress(int srcAddr)
{
    this->DataAddress=srcAddr;
}

void OutputConfigs::setOutputDataLength(int length)
{
    this->DataLength=length;
}

void OutputConfigs::setOutputStartAddr(int startAddr)
{
    this->StartAddr=startAddr;
}

QString OutputConfigs::getOutputSerialName()
{
    return this->SerialName;
}

int OutputConfigs::getOutputSlaveID()
{
    return this->SlaveID;
}

int OutputConfigs::getOutputDataAddress()
{
    return this->DataAddress;
}

int OutputConfigs::getOutputDataLength()
{
    return this->DataLength;
}

int OutputConfigs::getOutputStartAddr()
{
    return this->StartAddr;
}

void OutputConfigs::connected()
{
    connect++;
    if(connect>10000)
        connect=10000;
}

void OutputConfigs::loadFromOutputs(QString file, QList<OutputConfigs *> *outputs)
{
    QSettings setting(file,QSettings::IniFormat);
    int i;
    int cnt=setting.beginReadArray("OutputConfig");
    outputs->clear();
    for(i=0;i<cnt;i++)
    {
        setting.setArrayIndex(i);
        OutputConfigs *oc=new OutputConfigs();
        QString name=setting.value("OutputSerial").toString();
        int id=setting.value("OutputSlaveID").toInt();
        int srcAddr=setting.value("OutputDataAddress").toInt();
        int length=setting.value("OutputDataLength").toInt();
        int startAddr=setting.value("OutputStartAdd").toInt();

        oc->setOutputInfo(name,id,srcAddr,length,startAddr);
        outputs->append(oc);
    }
}

void OutputConfigs::loadFromOutputs(QString file, QTableWidget *tw, QList<OutputConfigs *> *outputs)
{
    QSettings setting(file,QSettings::IniFormat);
    int i;
    int cnt=setting.beginReadArray("OutputConfig");
    outputs->clear();
    for(i=0;i<cnt;i++)
    {
        setting.setArrayIndex(i);
        OutputConfigs *oc=new OutputConfigs();
        QString name=setting.value("OutputSerial").toString();
        int id=setting.value("OutputSlaveID").toInt();
        int srcAddr=setting.value("OutputDataAddress").toInt();
        int length=setting.value("OutputDataLength").toInt();
        int startAddr=setting.value("OutputStartAdd").toInt();
        oc->setOutputInfo(name,id,srcAddr,length,startAddr);
        outputs->append(oc);
        tw->insertRow(tw->rowCount());
        tw->setItem(i,0,new QTableWidgetItem(name));
        tw->setItem(i,1,new QTableWidgetItem(QString::number(id)));
        tw->setItem(i,2,new QTableWidgetItem(QString::number(srcAddr)));
        tw->setItem(i,3,new QTableWidgetItem(QString::number(length)));
        tw->setItem(i,4,new QTableWidgetItem(QString::number(startAddr)));
    }
}

void OutputConfigs::saveToOutputs(QString file, QTableWidget *tw)
{
    int cnt=tw->rowCount(),i=0;
    QString port;
    int id,srcAddr,length,startAdd;
    QSettings setting(file,QSettings::IniFormat);
    setting.beginWriteArray("OutputConfig");
    for(i=0;i<cnt;i++)
    {
        if(tw->item(i,0)==NULL)
        {
            tw->removeRow(i);
            continue;
        }
        port=tw->item(i,0)->data(0).toString();
        id=tw->item(i,1)->data(0).toInt();
        srcAddr=tw->item(i,2)->data(0).toInt();
        length=tw->item(i,3)->data(0).toInt();
        startAdd=tw->item(i,4)->data(0).toInt();
        setting.setArrayIndex(i);
        setting.setValue("OutputSerial",port);
        setting.setValue("OutputSlaveID",id);
        setting.setValue("OutputDataAddress",srcAddr);
        setting.setValue("OutputDataLength",length);
        setting.setValue("OutputStartAdd",startAdd);
    }
    setting.endArray();
}


